package com.sghd.fight.module.game.config;

import com.sghd.common.ramcache.IEntity;
import com.sghd.common.ramcache.PerPersist;
import com.sghd.common.ramcache.anno.Cached;
import com.sghd.common.ramcache.anno.CachedSizes;
import com.sghd.common.ramcache.anno.Persister;
import com.sghd.common.ramcache.anno.Persisters;
import com.sghd.common.resource.script.ScriptHelper;
import com.sghd.common.resource.script.ScriptType;
import com.sghd.common.resource.script.THScript;
import com.sghd.common.utils.json.JsonUtils;
import com.sghd.pojo.fight.game.UnitValue;

import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author frank
 * @date 2018-10-23 10:59
 * @desc buff配置
 **/
@Entity
@Table(catalog = "bsdld_fight")
@Cached(size = CachedSizes.DEFAULT, persister = @Persister(Persisters.PRE_HOUR))
public class BuffConfig implements IEntity<Integer>, PerPersist {
    @Id
    private int id;
    /**修改的属性*/
    @Enumerated(EnumType.STRING)
    private UnitValue value;
    /**公式上下文*/
    @Transient
    private Map<String, Object> context;
    @Lob
    private String contextJson;
    /** 公式*/
    @Lob
    private String formula;
    /***/
    @Lob
    private String timeFormula;
    @Transient
    private THScript script;
    @Transient
    private THScript timeScript;
    /** 准确步进*/
    private int coolVal;
    /** 描述*/
    private String description;

    public Integer getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public UnitValue getValue() {
        return value;
    }

    public void setValue(UnitValue value) {
        this.value = value;
    }

    public Map<String, Object> getContext() {
        return context;
    }

    public void setContext(Map<String, Object> context) {
        this.context = context;
    }

    public int calc(Map<String, Object> ctx) {
        Number result = (Number) script.execute(ctx);
        return result.intValue();
    }

    public int calcTime(Map<String, Object> ctx) {
        Number result = (Number) timeScript.execute(ctx);
        return result.intValue();
    }

    public String getFormula() {
        return formula;
    }

    public void setFormula(String formula) {
        this.formula = formula;
    }

    public THScript getScript() {
        return script;
    }

    public void setScript(THScript script) {
        this.script = script;
    }

    public String getTimeFormula() {
        return timeFormula;
    }

    public void setTimeFormula(String timeFormula) {
        this.timeFormula = timeFormula;
    }

    public THScript getTimeScript() {
        return timeScript;
    }

    public void setTimeScript(THScript timeScript) {
        this.timeScript = timeScript;
    }

    public String getContextJson() {
        return contextJson;
    }

    public void setContextJson(String contextJson) {
        this.contextJson = contextJson;
    }

    public int getCoolVal() {
        return coolVal;
    }

    public void setCoolVal(int coolVal) {
        this.coolVal = coolVal;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public void afterLoad() {
        context = JsonUtils.string2Map(contextJson);
    }

    @Override
    public void beforePersist() {
    }

    @Transient
    public BuffConfig copy() {
        BuffConfig config = new BuffConfig();
        config.id = id;
        config.value = value;
        config.context = new HashMap<>(context);
        config.script = ScriptHelper.create(formula, ScriptType.JEXL);
        config.coolVal = coolVal;
        config.timeScript = ScriptHelper.create(timeFormula, ScriptType.JEXL);
        return config;
    }
}
